package master.ghrm.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Version;

import org.apache.commons.collections.CollectionUtils;

@Entity
public class EvalCategory extends AbstractBaseEntity {

	private static final long serialVersionUID = 1L;

	public static final int LENGTH_DESCRIPTION = 1000;
	public static final int LENGTH_SUBJECT = 1000;
	public static final int LENGTH_NAME = 1000;

	@Version
	private Long version;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "eval_category_id")
	private Integer evalCategoryId;

	@Column(nullable = false, length = LENGTH_NAME)
	private String name;

	@Column(nullable = false, length = LENGTH_SUBJECT)
	private String subject;

	@Column(length = LENGTH_DESCRIPTION)
	private String description;

	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinTable(name = "tbl_questioneer_category", joinColumns = { @JoinColumn(name = "eval_category_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "questioneer_id", nullable = false, updatable = false) })
	private List<Questioneer> questioneers;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "evalCategory")
	private List<Question> questions = new ArrayList<Question>(0);

	@Column(name = "showing_pdp")
	private boolean showingPdp;

	@Column(name = "needs_elaboration")
	private boolean needsElaboration;

	/**
	 * Default constructor, persistence use.
	 */
	public EvalCategory() {
	}

	/**
	 * @param aName name.
	 * @param text text.
	 */
	public EvalCategory(String aName, String aSubject, String aDescription) {
		this.name = aName;
		this.subject = aSubject;
		this.description = aDescription;
	}

	/**
	 * @return
	 */
	public Long getVersion() {
		return version;
	}

	/**
	 * @param aVersion
	 */
	public void setVersion(Long aVersion) {
		this.version = aVersion;
	}

	/**
	 * @return
	 */
	public Integer getEvalCategoryId() {
		return evalCategoryId;
	}

	/**
	 * @param anEvalCategoryId
	 */
	public void setEvalCategoryId(Integer anEvalCategoryId) {
		this.evalCategoryId = anEvalCategoryId;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param aName
	 */
	public void setName(String aName) {
		this.name = aName;
	}

	/**
	 * @return
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param aDescription
	 */
	public void setDescription(String aDescription) {
		this.description = aDescription;
	}

	/**
	 * @return questioneers.
	 */
	public List<Questioneer> getQuestioneers() {
		return this.questioneers;
	}

	/**
	 * @return subject
	 */
	public String getSubject() {
		return subject;
	}

	/**
	 * @param aSubject
	 */
	public void setSubject(String aSubject) {
		this.subject = aSubject;
	}

	/**
	 * @param aQuestioneers questioneers.
	 */
	public void setQuestioneers(List<Questioneer> aQuestioneers) {
		this.questioneers = aQuestioneers;
	}

	/**
	 * Add a questioneer.
	 * 
	 * @param questioneer questioneer
	 */
	public void addQuestioneer(Questioneer questioneer) {
		if (questioneer != null) {
			this.questioneers.add(questioneer);
		}
	}

	/**
	 * @return
	 */
	public List<Question> getQuestions() {
		return questions;
	}

	/**
	 * @param questionList
	 */
	public void setQuestions(List<Question> questionList) {
		this.questions = questionList;
	}

	/**
	 * @param question
	 */
	public void addQuestion(Question question) {
		if (question != null) {
			this.questions.add(question);
		}
	}

	public void removeQuestion(Question question) {
		questions.remove(question);
	}

	/**
	 * @return the whether or not category shows PDP Reminder section.
	 */
	public boolean isShowingPdp() {
		return showingPdp;
	}

	/**
	 * @param showPdp whether or not category shows PDP Reminder section.
	 */
	public void setShowingPdp(boolean showPdp) {
		this.showingPdp = showPdp;
	}

	/**
	 * @return the needsElaboration
	 */
	public boolean isNeedsElaboration() {
		return needsElaboration;
	}

	/**
	 * @param doesNeedElaboration the needsElaboration to set
	 */
	public void setNeedsElaboration(boolean doesNeedElaboration) {
		this.needsElaboration = doesNeedElaboration;
	}

	public Boolean isLocked() {
		Boolean isLocked = Boolean.FALSE;

		if (CollectionUtils.isNotEmpty(questioneers)) {
			for (Questioneer questioneer : questioneers) {
				// List<Evaluation> evaluations = questioneer.getEvaluations();
				// if (CollectionUtils.isNotEmpty(evaluations)) {
				// for (Evaluation evaluation : evaluations) {
				// isLocked = evaluation.getPublished();
				//
				// if (isLocked) {
				// break;
				// }
				// }
				// }

				if (isLocked) {
					break;
				}
			}
		}

		return isLocked;
	}

	/**
	 * @see com.levi9.hrm.model.base.AbstractBaseEntity#getExcludedAttributes()
	 */
	@Override
	protected String[] getExcludedAttributes() {
		return new String[] { "questioneers" };
	}
}
